﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using AioNightly.Model;
using AioNightly.Dao;
using AioNightly.Common;

namespace AioNightly
{
    public partial class Note : System.Web.UI.Page
    {
        private Item m_item;
        private User m_user;

        protected void Page_Load(object sender, EventArgs e)
        {
            CheckUser();
            RefreshItem();
            RefreshCommentRepeater();
            RefreshAttachRepeater();
        }

        private void CheckUser()
        {
            m_user = (User)Session["User"];
            
            if (m_user==null)
            {
                Response.Redirect("~/Account/Login.aspx");
            }
        }

        private void GetItem()
        {
            string id = Request.QueryString["note"];
            if (id!=null)
            {
                ItemDAO m_itemDAO = new ItemDAO();
                m_item = m_itemDAO.GetItemByID(Int32.Parse(id));
            }

            if (m_user!=null && m_item!=null)
            {
                if (m_user.Type==(int)UserManager.UserType.Dev)
                {
                    if (m_user.Id != m_item.AuthorId)
                    {
                        bt_edititem.Visible = false;
                    }
                }
            }
        }

        private void RefreshItem()
        {
            GetItem();
            if (m_item!=null)
            {
                lb_item.Text = m_item.Content;

                UserDAO m_userDAO = new UserDAO();
                string username = m_userDAO.GetUsernameByItemId(m_item.Id);
                lb_author.Text = username + ":  ";

                switch (m_item.State)
                {
                    case (int)TypeManager.ItemState.Rejected:
                            lb_item.ForeColor = System.Drawing.Color.Red;
                	    break;

                    case (int)TypeManager.ItemState.Verified:
                        lb_item.ForeColor = System.Drawing.Color.Green;
                        break;

                    case (int)TypeManager.ItemState.Unchecked:
                        lb_item.ForeColor = System.Drawing.Color.Black;
                        break;

                    default:
                        break;
                }
            }
        }

        private void RefreshCommentRepeater()
        {
            if (m_item!=null)
            {
                CommentDAO m_commentDAO = new CommentDAO();
                Comment[] m_commentArray = m_commentDAO.GetCommentByItemId(m_item.Id).ToArray();
                rp_comment.DataSource = m_commentArray;
                rp_comment.DataBind();
            }
        }

        protected void rp_comment_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                ((Label)e.Item.FindControl("lb_comment")).Text = ((Comment)e.Item.DataItem).Content;

                UserDAO m_userDAO = new UserDAO();
                string m_username = m_userDAO.GetUsernameById(((Comment)e.Item.DataItem).AuthorId);
                ((Label)e.Item.FindControl("lb_author")).Text = m_username + "    " + ((Comment)e.Item.DataItem).CreateTime;
            }
        }

        protected void bt_addcomment_Click(object sender, EventArgs e)
        {
            if (m_item!=null && m_user!=null)
            {
                string m_addcomment = tb_addcomment.Text.Trim();
                if (m_addcomment.Length > 0)
                {
                    CommentDAO m_commentDAO = new CommentDAO();
                    m_commentDAO.AddComment(m_item.Id, m_addcomment, m_user.Id);

                    RefreshCommentRepeater();

                    tb_addcomment.Text = "";
                }
            }
        }

        protected void bt_edititem_Click(object sender, EventArgs e)
        {
            SwitchVisible();

            tb_edititem.Text = lb_item.Text;
        }

        private void SwitchVisible()
        {
            if (m_user!=null)
            {

                bt_submitedit.Visible = !bt_submitedit.Visible;
                bt_canceledit.Visible = !bt_canceledit.Visible;

                switch (m_user.Type)
                {
                    case (int)UserManager.UserType.Master:
                        bt_edititem.Visible = !bt_edititem.Visible;
                        ddl_itemstate.Visible = !ddl_itemstate.Visible;
                        tb_edititem.Visible = !tb_edititem.Visible;

                        bt_delitem.Visible = !bt_delitem.Visible;

                        if (ddl_itemstate.Visible && m_item != null)
                        {
                            ddl_itemstate.SelectedIndex = m_item.State;
                        }
                        break;

                    case (int)UserManager.UserType.Dev:
                        bt_edititem.Visible = !bt_edititem.Visible;
                        tb_edititem.Visible = !tb_edititem.Visible;
                        bt_delitem.Visible = !bt_delitem.Visible;
                        ddl_itemstate.SelectedIndex = m_item.State;
                        break;

                    case (int)UserManager.UserType.Qa:
                        ddl_itemstate.Visible = !ddl_itemstate.Visible;
                        if (ddl_itemstate.Visible && m_item != null)
                        {
                            ddl_itemstate.SelectedIndex = m_item.State;
                        }
                        break;

                    default:
                        break;
                }
            }
        }

        protected void bt_canceledit_Click(object sender, EventArgs e)
        {
            SwitchVisible();
        }

        protected void bt_submitedit_Click(object sender, EventArgs e)
        {
            if (m_item != null)
            {
                string m_edititem = tb_edititem.Text.Trim();
                if (m_edititem.Length > 0)
                {
                    ItemDAO m_itemDAO = new ItemDAO();
                    m_itemDAO.EditItemContent(m_item.Id, m_edititem, ddl_itemstate.SelectedIndex);

                    RefreshItem();

                    lb_item.Text = m_edititem;
                    tb_edititem.Text = "";
                    SwitchVisible();
                }
            }
        }

        protected void bt_delitem_Click(object sender, EventArgs e)
        {
            if (m_item!=null)
            {
                int productId = m_item.BuildId;

                ItemDAO m_itemDAO = new ItemDAO();
                m_itemDAO.DelItemById(m_item.Id);
            }
        }

        protected void bt_addattach_Click(object sender, EventArgs e)
        {
            if (fu_attach.HasFile && m_item!=null)
            {
                string m_time = TimeManager.GetSystemTime();
                string m_timeDir = GetTimeDir(m_time);
                string m_dir = UserManager.GetWebRoot() + m_timeDir + @"\";

                if (!Directory.Exists(m_dir))
                {
                    Directory.CreateDirectory(m_dir);
                }
                fu_attach.SaveAs(m_dir+fu_attach.FileName);

                AttachDAO m_attachDAO = new AttachDAO();
                m_attachDAO.AddAttach(m_item.Id, fu_attach.FileName, m_time);

                RefreshAttachRepeater();
            }
        }

        private string GetTimeDir(string str)
        {
            str = str.Replace("-", "_");
            str = str.Replace(":", "_");
            str = str.Replace(" ", "_");
            str = str.Replace(@"/", "_");
            return str;
        }

        protected void rp_attach_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                ((HyperLink)e.Item.FindControl("ht_attach")).Text = string.Format("{0}", ((Attach)e.Item.DataItem).Path);
                ((HyperLink)e.Item.FindControl("ht_attach")).NavigateUrl = string.Format("{0}", @"Attach/" + GetTimeDir(((Attach)e.Item.DataItem).CreateTime) + @"/" + ((Attach)e.Item.DataItem).Path);
            }
        }

        private void RefreshAttachRepeater()
        {
            if (m_item!=null)
            {
                AttachDAO m_attachDAO = new AttachDAO();
                Attach[] m_attachArray = m_attachDAO.GetAttachByItemId(m_item.Id).ToArray();

                rp_attach.DataSource = m_attachArray;
                rp_attach.DataBind();
            }
        }
    }
}